package com.monas.client.dialog;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import com.monas.client.util.UIHelper;

public class EditUserDialog extends AbstractUserDialog {

	private static final String MESSAGE = "Please specify new username and password";

	private Text confirmText;

	private Text remarkText;

	private String remark = null;

	private final String userName;

	public EditUserDialog(Shell parentShell, String username, String remark) {
		super(parentShell, "Edit User Form");
		userName = username;
		this.remark = remark;
	}

	@Override
	protected Control createContents(Composite parent) {
		Control contents = createNewContents(parent, MESSAGE);

		return contents;
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		composite.setLayoutData(UIHelper.fillBoth());
		composite.setLayout(new GridLayout(1, false));

		createUsername(composite);
		usernameText.setText(userName);
		usernameText.setEnabled(false);

		passwordText = createPassword(composite, "New Password:");
		confirmText = createPassword(composite, "Confirm Password:");

		createLabel(composite, "Remark:");
		remarkText = createText(composite);
		remarkText.setText(remark);
		remarkText.addModifyListener(new ModifyListener() {

			@Override
			public void modifyText(ModifyEvent e) {
				remark = remarkText.getText();
			}
		});

		return composite;
	}

	@Override
	protected void checkCompleted() {
		setErrorMessage(null);
		setMessage(MESSAGE);
		super.checkCompleted();

		if (ok != null) {
			if (ok.isEnabled()) {
				if (confirmText.getText().length() > 0) {
					if (!confirmText.getText().equals(passwordText.getText())) {
						error("Confirmation isn't match with password");
						return;
					}
				} else {
					error("Please define confirm password!");
					return;
				}
			}
		}
	}

	public String getRemark() {
		if (remark == null) {
			remark = "";
		}
		return remark;
	}

}
